/*bookManage页面 多选删除事件*/
function delSelect() {
    var ids = "";
    var rows = $('#table').bootstrapTable('getSelections');

    if (rows.length === 0) {
        swal("请选择需要删除的记录！",{icon: "warning",timer:1500})
    } else {
        $(rows).each(function () {
            ids += this.id + ',';
        });
        $.ajax({
            url:"deleteBookByIds",
            type: "post",
            dataType: "json",
            data: {"ids":ids},
            success: function (result){
                if (result.msg==="ok"){
                    swal("删除成功！",{icon: "success",timer:1500})
                    location.reload()
                }
            },
            error:function (){
                swal("删除失败，请稍后再试！",{icon: "error",timer:1500})
            }
        })
    }
}

/*检查isbn号是否重复*/
function checkIsbn() {
    var isbn=$("#isbn").val()
    var isbnMsg={
        color:'red',
        display:'block',
        textAlign:'left',
    }
    if (isbn === "" || isbn === null){
        $("#err-isbn").text("请输入isbn！")
        $("#err-isbn").css(isbnMsg)
        $("#isbn")[0].focus()
    }else {
        $.ajax({
            url:"searchIsbn",
            type: "post",
            dataType: "json",
            data: {"isbn": isbn},
            success: function (result){
                if (result.msg !== "ok"){
                    $("#err-isbn").text("isbn重复，请重新填写！")
                    $("#err-isbn").css(isbnMsg)
                    $("#isbn")[0].focus()
                }else {
                    $("#err-isbn").text("").css("display","none")
                }
            }
        })
    }
}

/*bookManage页面添加图书事件*/
function addBookInfo() {
    var name=$("#name").val()
    var oldPrice=$("#oldPrice").val()
    var newPrice=$("#newPrice").val()
    var author=$("#author").val()
    var publisher=$("#publisher").val()
    var authorLoc=$("#authorLoc").val()
    var publishDate=$("#publishDate").val()
    if(name === "" || oldPrice === "" || newPrice === "" ||author === ""  ||publisher === ""  || authorLoc === "" || publishDate === "" ){
        swal("请填写完整的信息！",{icon: "error",timer:1500})
    }else {
        $.ajax({
            url:"addBook",
            type: "post",
            dataType: "json",
            data: $("#model_addBook").serialize(),
            success: function (result){
                console.log(result)
                if (result.msg==="ok"){
                    swal("添加成功！",{icon: "success",timer:1500})
                    location.reload()
                }
            },
            error:function (){
                swal("添加失败，请稍后再试！",{icon: "error",timer:1500})
            }
        })
    }
}

/*bookManage页面修改图书信息事件*/
function updateBookInfo() {
    var name=$("#updateName").val()
    var oldPrice=$("#updateOldPrice").val()
    var newPrice=$("#updateNewPrice").val()
    var author=$("#updateAuthor").val()
    var publisher=$("#updatePublisher").val()
    var authorLoc=$("#updateAuthorLoc").val()
    var publishDate=$("#updatePublishDate").val()
    if(name === "" || oldPrice === "" || newPrice === "" ||author === ""  ||publisher === ""  || authorLoc === "" || publishDate === "" ){
        swal("请填写完整的信息！",{icon: "error",timer:1500})
    }else {
        $.ajax({
            url:"updateBook",
            type: "post",
            dataType: "json",
            data: $("#model_updateBookInfo").serialize(),
            success: function (result){
                console.log(result)
                if (result.msg==="ok"){
                    swal("修改成功！",{icon: "success",timer:1500})
                    location.reload()
                }
            },
            error:function (){
                swal("修改失败，请稍后再试！",{icon: "error",timer:1500})
            }
        })
    }
}

/*删除图书*/
function deleteBook(id) {
    $.ajax({
        url:"deleteBookById",
        type: "post",
        dataType: "json",
        data: {"id":id},
        success: function (result){
            if (result.msg==="ok"){
                swal("删除成功！",{icon: "success",timer:1500})
                location.reload()
            }
        },
        error:function (){
            swal("删除失败，请稍后再试！",{icon: "error",timer:1500})
        }
    })
}





/*用户模块*/
function checkUserName() {
    var username = $("#updateUsername").val()
    var userNameMsg={
        color:'red',
        display:'block',
        textAlign:'left',
    }
    if (username === "" || username === null){
        $("#err-username").text("请输入用户名！")
        $("#err-username").css(userNameMsg)
        $("#err-username")[0].focus()
    }else {
        $.ajax({
            url:"searchUserName",
            type: "post",
            dataType: "json",
            data: {"username": username},
            success: function (result){
                if (result.msg !== "ok"){
                    $("#err-username").text("用户名重复，请重新填写！")
                    $("#err-username").css(userNameMsg)
                    $("#err-username")[0].focus()
                }else {
                    $("#err-username").text("").css("display","none")
                }
            }
        })
    }
}

/*修改用户*/
function updateUser() {
    var username=$("#updateUsername").val()
    var password=$("#updatePassword").val()
    var email=$("#updateEmail").val()
    var phone=$("#updatePhone").val()
    var company=$("#updateCompany").val()
    if(username === "" || password === "" || email === "" || phone === "" || company === ""){
        swal("请填写完整的信息！",{icon: "error",timer:1500})
    }else {
        $.ajax({
            url:"updateUser",
            type: "post",
            dataType: "json",
            data: $("#model_updateUser").serialize(),
            success: function (result){
                console.log(result)
                if (result.msg==="ok"){
                    swal("修改成功！",{icon: "success",timer:1500})
                    location.reload()
                }
            },
            error:function (){
                swal("修改失败，请稍后再试！",{icon: "error",timer:1500})
            }
        })
    }
}

/*删除用户*/
/*删除单个*/
function deleteUserById(id) {
    $.ajax({
        url:"deleteUserById",
        type: "post",
        dataType: "json",
        data: {"id":id},
        success: function (result){
            if (result.msg==="ok"){
                swal("删除成功！",{icon: "success",timer:1500})
                location.reload()
            }
        },
        error:function (){
            swal("删除失败，请稍后再试！",{icon: "error",timer:1500})
        }
    })
}

/*删除多个*/
function delSelectUser() {
    var ids = "";
    var rows = $('#table').bootstrapTable('getSelections');

    if (rows.length === 0) {
        swal("请选择需要删除的记录！",{icon: "warning",timer:1500})
    }else {
        $(rows).each(function () {
            ids += this.id + ',';
        });
        $.ajax({
            url:"deleteUserByIds",
            type: "post",
            dataType: "json",
            data: {"ids":ids},
            success: function (result){
                if (result.msg==="ok"){
                    swal("删除成功！",{icon: "success",timer:1500})
                    location.reload()
                }
            },
            error:function (){
                swal("删除失败，请稍后再试！",{icon: "error",timer:1500})
            }
        })
    }
}




/*订单事件*/
/*修改订单发货的状态*/
function updateOrderDelivery(id,status) {
    if (status === '1'){
        swal({
            title: "提示信息",
            text: "用户未付款，是否确定发货？",
            icon: "warning",
            dangerMode: true,
            buttons: {
                cancel: "取消",
                defeat: "确定",
            }
        }).then((value) => {
            switch (value) {
                case "defeat":
                    deliver(id)
                    break;
                default:
            }
        });
    }else {
        deliver(id)
    }
}

function deliver(id) {
    $.ajax({
        url:"updateDeliveryStatus",
        type: "post",
        dataType: "json",
        data: {"id":id},
        success: function (result){
            if (result.msg==="ok"){
                swal("发货成功！",{icon: "success",timer:1500})
                location.reload()
            }else {
                swal("发货失败，请稍后再试！",{icon: "error",timer:1500})
            }
        }
    })
}

/*去订单详情页面*/
function toDetailPage(id) {
    location.href = "findOrderById?id=" + id;
}


/*修改管理员事件*/
/*检查管理员名字*/
function checkAdminName() {
    var name = $("#adminName").val()
    var adminNameMsg={
        color:'red',
        display:'block',
        textAlign:'left',
    }
    if (name === "" || name === null){
        $("#err-adminName").text("请输入用户名！")
        $("#err-adminName").css(adminNameMsg)
        $("#err-adminName")[0].focus()
    }else {
        $.ajax({
            url:"searchAdminName",
            type: "post",
            dataType: "json",
            data: {"name": name},
            success: function (result){
                if (result.msg != "ok"){
                    $("#err-adminName").text("用户名重复，请重新填写！")
                    $("#err-adminName").css(adminNameMsg)
                    $("#err-adminName")[0].focus()
                }else {
                    $("#err-adminName").text("").css("display","none")
                }
            }
        })
    }
}

/*添加管理员*/
function addAdmin() {
    var password=$("#adminPas").val()
    var email=$("#adminEmail").val()
    var phone=$("#adminPhone").val()
    var address=$("#adminAddress").val()
    if(address === "" || password === "" || email === "" || phone === ""){
        swal("请填写完整的信息！",{icon: "error",timer:1500})
    }else {
        $.ajax({
            url:"addAdmin",
            type: "post",
            dataType: "json",
            data: $("#model_addAdmin").serialize(),
            success: function (result){
                if (result.msg==="ok"){
                    swal("添加成功！",{icon: "success",timer:1500})
                    location.reload()
                }
            },
            error:function (){
                swal("添加失败，请稍后再试！",{icon: "error",timer:1500})
            }
        })
    }
}


/*修改管理员*/
function updateAdmin() {
    var password=$("#updateAdminPas").val()
    var name=$("#updateAdminName").val()
    var email=$("#updateAdminEmail").val()
    var phone=$("#updateAdminPhone").val()
    var address=$("#updateAdminAddress").val()
    if(address === "" || password === "" || email === "" || phone === "" || name === ""){
        swal("请填写完整的信息！",{icon: "error",timer:1500})
    }else {
        $.ajax({
            url:"updateAdmin",
            type: "post",
            dataType: "json",
            data: $("#model_updateAdmin").serialize(),
            success: function (result){
                if (result.msg==="ok"){
                    swal("修改成功！",{icon: "success",timer:1500})
                    location.reload()
                }
            },
            error:function (){
                swal("修改失败，请稍后再试！",{icon: "error",timer:1500})
            }
        })
    }
}

/*删除管理员*/
function delAdmin(id) {
    $.ajax({
        url:"deleteAdminById",
        type: "post",
        dataType: "json",
        data: {"id":id},
        success: function (result){
            if (result.msg==="ok"){
                swal("删除成功！",{icon: "success",timer:1500})
                location.reload()
            }
        },
        error:function (){
            swal("删除失败，请稍后再试！",{icon: "error",timer:1500})
        }
    })
}





/*管理界面事件*/
/*修改用户信息的事件*/
function updateInfo(){
    if ($("#phoneErro").text()!==""){
        swal("号码格式不对哦！",{icon: "error",timer:1500})
        return ;
    }
    $.ajax({
        url:"/admin/updateAdmin",
        type: "post",
        dataType: "json",
        data:$("#model_form1").serialize(),
        success:function (result){
            if (result.msg==="ok"){
                $("#model_form1").modal('hide')
                swal("修改成功！",{icon: "success",timer:1500})
                location.reload()
            }
        },
        error:function (){
            swal("修改失败！",{icon: "error",timer:1500})
        }
    })
}



//修改密码
function updatePass(){
    var oldPass=$("#oldPass").val()
    var newPass=$("#newPass").val()
    var newPass1=$("#newPass1").val()
    if(oldPass===""||newPass===""||newPass1===""){
        swal("输入密码不为空！",{icon:"error",timer:1800})
        return false;
    } else if (newPass!==newPass1){
        swal("两次密码不匹配！",{icon:"error",timer:1800})
        return false;
    }else {
        $.ajax({
            url:"/admin/updateAdmin",
            type: "post",
            dataType: "json",
            data:$("#model_form2").serialize(),
            success:function (result){
                if (result.msg==="ok"){
                    $("#oldPass").val("")
                    $("#newPass").val("")
                    $("#newPass1").val("")
                    $("#updatePass").modal('hide')
                    swal("修改成功！",{icon: "success",timer:1500})
                }
                if (result.msg==="PassError"){
                    $("#oldPass").val("")
                    $("#newPass").val("")
                    $("#newPass1").val("")
                    swal("原密码错误，修改失败！",{icon: "error",timer:1500})
                }
            },
            error:function (){
                $("#oldPass").val("")
                $("#newPass").val("")
                $("#newPass1").val("")
                swal("修改失败！",{icon: "error",timer:1500})
            }
        })
        return true;
    }

}

//检验数据格式
function checkPhone() {
    var phoneMsg={
        color:'red',
        display:'block',
        textAlign:'left',
    }
    var phoneReg=/^[1][3,4,5,7,8][0-9]{9}$/
    var phone=$("#phone").val()
    if(!phoneReg.test(phone)){
        $("#phoneErro").text("手机号码格式有误！")
        $("#phoneErro").css(phoneMsg)
        $("#phone")[0].focus()
    }else {
        $("#phoneErro").text("")
    }
}
function checkPhone1() {
    var phoneMsg={
        color:'red',
        display:'block',
        textAlign:'left',
    }
    var phoneReg=/^[1][3,4,5,7,8][0-9]{9}$/
    var phone=$("#updatephone").val()
    if(!phoneReg.test(phone)){
        $("#phoneError").text("手机号码格式有误！")
        $("#phoneError").css(phoneMsg)
        $("#updatephone")[0].focus()
    }else {
        $("#phoneError").text("")
    }
}
function checkPhone2() {
    var phoneMsg={
        color:'red',
        display:'block',
        textAlign:'left',
    }
    var phoneReg=/^[1][3,4,5,7,8][0-9]{9}$/
    var phone=$("#phone1").val()
    if(!phoneReg.test(phone)){
        $("#phoneEr").text("手机号码格式有误！")
        $("#phoneEr").css(phoneMsg)
        $("#phone1")[0].focus()
    }else {
        $("#phoneEr").text("")
    }
}


